package com.onland.dfish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.onland.dfish.entity.Users;
import com.onland.dfish.entity.Videos;
import com.onland.dfish.mapper.UsersMapper;
import com.onland.dfish.service.IUsersService;
import com.onland.dfish.util.EncryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("usersService")
public class UsersServiceImpl  implements IUsersService {
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public int countByEmail(String email) {
        QueryWrapper<Users> wrapper=new QueryWrapper<>();
        wrapper.eq("email",email);
        return usersMapper.selectCount(wrapper);
    }

    @Override
    public int createUsers(String email, String password) {
        Users users=new Users();
        users.setEmail(email);
        //密码需要加密,不可以破解
        password=encryptPwd(password);
        users.setPsd(password);
        users.setAddTime(new Date());//注册时间
        return usersMapper.insert(users);
}

    /**
     * 自定义加密算法
     * @param password
     * @return
     */
    private String encryptPwd(String password){
       for(int c=1;c<=5;c++){
           password=EncryptUtils.getMD5(password+"https://www.douyu.com?i="+c);
       }
       return password;
    }

    @Override
    public Users login(String email, String password) {
        QueryWrapper<Users> wrapper=new QueryWrapper<>();
        wrapper.eq("email",email);
        password=encryptPwd(password);
        wrapper.eq("password",password);
        return usersMapper.selectOne(wrapper);
    }

    /**
     * 根据用户id查询个人信息
     *
     * @param id
     * @return
     */
    @Override
    public Users selectById(int id) {
        return usersMapper.selectById(id);
    }

    /**
     * 修改用户资料
     *
     * @param users
     * @return
     */
    @Override
    public int updateUser(Users users) {
        return usersMapper.updateById(users);
    }

    /**
     * 根据id修改密码
     *
     * @param users
     */
    @Override
    public int UpUserPasswd(Users users) {
        return usersMapper.updateById(users);
    }

    /**
     * 找回密码
     * @param password
     * @return
     */
    @Override
    public int updatePasswordByCode(String password,String email) {
        String psd=encryptPwd(password);
        return usersMapper.updatePasswordByCode(psd,email);
    }

    /**
     * 查询关注
     *
     * @param userId
     * @return
     */
    @Override
    public List<Users> queryLike(int userId) {
        return usersMapper.queryLike(userId);
    }

    /**
     * 查询粉丝
     *
     * @param userId
     * @return
     */
    @Override
    public List<Users> queryFans(int userId) {
        return usersMapper.queryFans(userId);
    }

    /**
     * 检查密码
     * @param password
     * @return
     */
    @Override
    public Users checkPwd(String password) {
        QueryWrapper<Users> wrapper=new QueryWrapper<>();
        password=encryptPwd(password);
        wrapper.eq("password",password);
        return usersMapper.selectOne(wrapper);
    }
}
